import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @description： 消费者1 只消费路由key为error的信息
 * @Author MRyan
 * @Date 2020/11/9 17:04
 * @Version 1.0
 */

public class Customer1 {
    public static void main(String[] args) throws IOException {
        //获取连接对象
        Connection connection = RabbitMqUtil.getConnection("192.168.43.78", 5672, "/ems", "ems", "123");
        //获取通道对象
        Channel channel = connection.createChannel();
        //通过通道声明交换机和交换机类型
        channel.exchangeDeclare("logs_direct", "direct");
        //声明一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //基于routingKey 绑定队列和交换机
        channel.queueBind(queue, "logs_direct", "error");
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1：" + new String(body));
            }
        });

    }
}
